import storage from "@/utils/Storage"
import { login } from "@/request/api/account"
const state = {
    walletAddress: storage.get('walletAddress') || "",
    token: storage.get('token') || "",
}

const mutations = {
    //钱包地址设置
    SET_WALLET_ADDRESS: (state, data) => {
        state.walletAddress = data
        storage.set('walletAddress', data);
    },
    //设置token
    SET_TOKEN: (state, data) => {
        state.token = data
        storage.set('token', data);
    },

    CLEAR: (state, data) => {
        state.walletAddress = ''
        state.token = ''
        storage.clear()
    },
}

const actions = {
    //钱包地址设置
    set_wallet_address({
        commit
    }, data) {
        commit('SET_WALLET_ADDRESS', data)
    },

    //token设置
    set_token({
        commit
    }, data) {
        commit('SET_TOKEN', data)
    },

    //删除所有缓存
    clear({
        commit
    }) {
        commit('CLEAR', "")
    },


    /**
     * 登录
     * @param {*} param0 
     * @returns 
     */
    setLogin({ commit, state }, params) {
        return new Promise((resolve, reject) => {
            login(params)
                .then((response) => {
                    if (response.code === 1) {
                        resolve(response.data)
                    } else {
                        reject(response.msg)
                    }
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}